function initializePersonal() {
    let userId;

    function fetchUserInfo() {
        $.ajax({
            url: `http://localhost:8080/user/get/${userId}`,
            type: 'GET',
            dataType: 'json',
            success: function(response) {
                if(response.message == "ok") {
                    $('#username').val(response.data.username);
                    $('#email').val(response.data.mailbox || '');
                    $('#password').val(response.data.password);
                    $('#institution').val(response.data.school || '');
                    $('#personal-info').show();
                }
                else{
                    alert(response.message);
                }

            },
        });
    }

    $('#loadInfo').on('click', function() {
        userId = $('#userId').val();
        fetchUserInfo();
    });

    $('form').on('submit', function(e) {
        e.preventDefault();
        const updatedUserData = {
            username: $('#username').val(),
            mailbox: $('#email').val(),
            password: $('#password').val(),
            school: $('#institution').val()
        };

        $.ajax({
            url: `http://localhost:8080/user/edit/${userId}`,
            type: 'PUT',
            data: JSON.stringify(updatedUserData),
            contentType: 'application/json',
            success: function(response) {
                if(response.message == "ok"){
                    $('#userId').val('');
                    $('form')[0].reset();
                    alert("编辑成功！");
                } else {
                    alert(response.message);
                }
            },
        });
    });

    $('#deleteUser').on('click', function() {
        $.ajax({
            url: `http://localhost:8080/user/delete/${userId}`,
            type: 'DELETE',
            dataType: 'json',
            success: function(response) {
                if(response.message == "ok") {
                    $('form')[0].reset();
                    $('#userId').val('');
                    alert("删除成功！");
                } else {
                    alert(response.message);
                }
            },
        });
    });
}

function bindPersonalEvents() {
    initializePersonal();
}